package com.ckzp.jfinal.system.controller;

import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.util.StringUtil;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.plugins.trans.TransData;
import com.ckzp.jfinal.system.model.SysRole;
import com.ckzp.jfinal.system.model.SysUser;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.core.util.DateUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.plugins.UpImage;
import com.ckzp.jfinal.system.model.SysActivity;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuyang(51594) on 2022-08-01 11:50:54.
 */
public class SysActivityController extends BaseController {

    @Admins(roles = "login", name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 列表显示页面
     */
    @Admins(roles = "system", name = "功能操作列表页", log = false)
    public void SysActivityList() {
        FormData fd = FormData.dao();

        FormSelect fs = FormSelect.n().setSource("`1`记录日志`0`不记录`").setDefaultString("全部").setCanNull(true).setName("is_log");
        fd.addSelect("islog", fs.showHtml());

        setAttr("form", fd);
        render("SysActivityList.html");
    }

    /**
     * 列表数据查询API
     * fd.addSelect("", SysDataCache.getFormSelect(""));
     */
    @Admins(roles = "system", name = "列表数据查询Api", log = false)
    public void SysActivityListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_activity");
        ps.orderby(getRequest(), "act_id desc");
        Page<SysActivity> pages = ps.paginate(SysActivity.dao);
        List<SysActivity> datalist = pages.getList();
        for (SysActivity act : datalist) {
            act.put("html_role", StringUtil.delEnd(act.mod.getStr("act_role_name"), ","));
            act.put("html_user", StringUtil.delEnd(act.mod.getStr("act_user_name"), ","));
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加功能操作Page
     */
    @Admins(roles = "system", name = "添加PAGE", log = false)
    public void AddSysActivity() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("act", new SysActivity());
        setAttr("form", fd);
        render("ModifySysActivity.html");
    }

    /**
     * 修改功能操作Page
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改功能操作Page", log = false)
    public void EditSysActivity() {
        String act_id = getPara("act_id", "");
        SysActivity o = SysActivity.dao.findById(act_id);
        FormData fd = FormData.dao().setActions("EDIT");
        if (o != null) {

        }
        setAttr("form", fd);
        setAttr("act", o);
        render("ModifySysActivity.html");
    }

    /**
     * 修改功能操作Api
     */
    @Admins(roles = "system", name = "修改功能操作Api", log = true)
    public void ModifySysActivityApi() {
// 取得数据
        Datas data = getParaData("postdata", "");
        String act_id = data.getStr("act_id");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysActivity.dao.findById(act_id)
                    .set("act_name", data.getStr("act_name"))
                    .set("act_desc", data.getStr("act_desc"))
                    .update();
            renderJson(Json(results, "修改"));
        }
    }


    /**
     * 列表显示页面
     */
    @Admins(roles = "system", name = "配置功能操作角色页", log = false)
    public void SetActivityRole() {
        int act_id = getParaToInt("act_id", 0);
        SysActivity activity = SysActivity.dao.findById(act_id);
        if (activity != null) {
            String[] act_role_list = StringUtil.split(activity.mod.getStr("act_role"), "|");
            List myrelist = new ArrayList();
            for (String roleid : act_role_list) {
                myrelist.add(roleid);
            }
            //取得系统所有角色
            List<SysRole> rolelist = SysRole.dao.findAll();
            List relist = new ArrayList();
            for (int k = 0; k < rolelist.size(); k++) {
                SysRole role = rolelist.get(k);
                TransData td = new TransData();
                td.setValue(role.getStr("role_id"));
                td.setTitle(role.getStr("role_name") + "(" + role.getStr("role_id") + ")");
                relist.add(td);
            }
            set("act", activity);
            set("all_role", toJsonStr(relist));
            set("select_role", toJsonStr(myrelist));
        }
        render("SetActivityRole.html");
    }


    /**
     * 设置用户角色api
     */
    @Admins(roles = "system", name = "配置功能操作角色Api", log = false)
    public void SetActivityRoleApi() {
        int act_id = getParaToInt("act_id", 0);
        SysActivity activity = SysActivity.dao.findById(act_id);
        if (activity != null) {
            String json_rolestr = getPara("role_str");
            List list = JsonUtil.json2List(json_rolestr, TransData.class);
            StringBuffer role_str = new StringBuffer();
            StringBuffer role_name_str = new StringBuffer();
            for (int k = 0; k < list.size(); k++) {
                TransData td = (TransData) list.get(k);
                String role = td.getValue();
                role_str.append(role + "|");
                role_name_str.append(td.getTitle() + ",");
            }
            activity.set("act_role", role_str.toString()).set("act_role_name", role_name_str.toString()).update();
            renderJson(JsonResult.n(0, "角色设置成功"));
        } else {
            renderJson(JsonResult.n(9, "活动不存在"));
        }
    }


    /**
     * 列表显示页面
     */
    @Admins(roles = "system", name = "配置功能操作用户页", log = false)
    public void SetActivityUser() {
        int act_id = getParaToInt("act_id", 0);
        SysActivity activity = SysActivity.dao.findById(act_id);
        if (activity != null) {
            String[] act_user_list = StringUtil.split(activity.mod.getStr("act_user"), "|");
            List myrelist = new ArrayList();
            for (String userid : act_user_list) {
                myrelist.add(userid);
            }
            //取得系统所有角色
            List<SysUser> userlist = SysUser.dao.findAll();
            List relist = new ArrayList();
            for (int k = 0; k < userlist.size(); k++) {
                SysUser user = userlist.get(k);
                TransData td = new TransData();
                td.setValue(user.getStr("emp_id"));
                td.setTitle(user.getStr("user_name"));
                relist.add(td);
            }
            set("act", activity);
            set("all_user", toJsonStr(relist));
            set("select_user", toJsonStr(myrelist));
        }
        render("SetActivityUser.html");
    }


    /**
     * 设置用户角色api
     */
    @Admins(roles = "system", name = "配置功能操作用户Api", log = false)
    public void SetActivityUserApi() {
        int act_id = getParaToInt("act_id", 0);
        SysActivity activity = SysActivity.dao.findById(act_id);
        if (activity != null) {
            String json_userstr = getPara("user_str");
            List list = JsonUtil.json2List(json_userstr, TransData.class);
            StringBuffer user_str = new StringBuffer();
            StringBuffer user_name_str = new StringBuffer();
            for (int k = 0; k < list.size(); k++) {
                TransData td = (TransData) list.get(k);
                String role = td.getValue();
                user_str.append(role + "|");
                user_name_str.append(td.getTitle() + ",");
            }
            activity.set("act_user", user_str.toString()).set("act_user_name", user_name_str.toString()).update();
            renderJson(JsonResult.n(0, "人员设置成功"));
        } else {
            renderJson(JsonResult.n(9, "活动不存在"));
        }
    }

    /**
     * 修改功能操作字段Api
     * demo:
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改功能操作字段Api", log = true)
    public void ModifySysActivityFieldApi() {
        String act_id = getParaStr("key_id", "");
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysActivity o = SysActivity.dao.findById(act_id);
        if (o != null) {
            boolean status = o.set(field, value).update();
            renderJson(JsonResult.n(status ? 0 : 9, "修改" + (status ? "成功" : "失败")));
        } else {
            renderJson(JsonResult.n(0, "修改成功"));
        }
    }


    /**
     * 删除功能操作Api
     */
    @Admins(roles = "system", name = "删除功能操作Api", log = true)
    public void DelSysActivityApi() {
        int act_id = getInt("act_id", 0);
        SysActivity act = SysActivity.dao.findById(act_id);
        if (act == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (act.delete()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
///////////////////////////////////
    }

    /**
     * 图片上传接口
     */
    @Admins(roles = "system", name = "图片上传API", log = false)
    public void UploadImages() {
        renderJson(UpImage.n().upload(getRequest(), "show", true, true));
    }


    /**
     * 改变功能操作字段状态Api(0 or 1)
     */
    @Admins(roles = "system", name = "改变功能操作字段状态Api", log = true)
    public void ChangeTagApi() {
        String keyid = getParaStr("key", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysActivity.dao, keyid, field, msg));
    }
}